package com.study.service.impl;

import com.study.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @desc:
 * @author: panxiyi
 * @date: 2023/5/7 11:15 下午
 */

@Service
public class SendMailServiceImpl2 implements SendMailService {

    @Autowired
    private JavaMailSender javaMailSender;

    // 发送人
    private String from = "2074992712@qq.com";
    // 接受人
    private String to = "pxy@meitu.com";
    // 标题
    private String title = "夏天-来了-标题";
    // 正文
    private String context = "<img src='https://bkimg.cdn.bcebos.com/pic/aec379310a55b319ebc4524ebef89526cffc1e17b9f4'><a href='http://www.baidu.com'>测试邮件正文标题</a>";
    @Override
    public void sendMail() {

        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 需要设置true MimeMessageHelper
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
            messageHelper.setFrom(from + "(peter's•pan)");
            messageHelper.setTo(to);
            messageHelper.setSubject(title);
            messageHelper.setText(context, true);

            // 添加附件
            File f1 = new File("/Users/mtpxy/IdeaProjects/pxycode/springboot/springboot_23_mail/src/main/resources/logo.png");
            messageHelper.addAttachment(f1.getName() + ".png", f1);

            File f2 = new File("/Users/mtpxy/IdeaProjects/pxycode/springboot/springboot_23_mail/target/springboot_23_mail-0.0.1-SNAPSHOT.jar");
            messageHelper.addAttachment("jar文件", f2);


            javaMailSender.send(mimeMessage);
            System.out.println("send end");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
}
